package CSound
{
	import UIFree.ObjectEvent;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class ps_player extends Object {
		private var _sound: Sound;
		protected var _channel: SoundChannel;
		private var _params: Array;
		private var _cardBody: Object;
		private var _volumeCtrl: ps_volume;
		private var _volume: Number;
		
		public function ps_player(cardBody: *, params: *) {
			super();
			_params = params;
			_cardBody = cardBody;
			if (_cardBody.loaded) doLoad()
			else _cardBody.app.addEventListener('COMPLETEFULLLOAD', doLoad);
		}
		
		protected function doLoad(e: ObjectEvent = null): void {
			if (_params[1]) {
				_volume = ps_sound.DEFVOLUME;
				_sound = new Sound(new URLRequest(_params[1]));
				_channel = _sound.play(0, 0, new SoundTransform(_volume));
				_volumeCtrl = new ps_volume(this);
			}
		}
		
		public function get volume(): Number {
			return _channel.soundTransform.volume;
		}
		
		public function set volume(a_volume: Number): void {
			_channel.soundTransform = new SoundTransform(a_volume);
		}
		
		public function dispose(): void {
			_volumeCtrl.dispose();
			if (_channel) _channel.stop();
			_cardBody.app.removeEventListener('COMPLETEFULLLOAD', doLoad);
		}
		
		public function get card(): Object {
			return _cardBody;
		}		
	}
}